package compiler;


import grammar.FunBaseVisitor;
import grammar.FunParser;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;

public class TestCompileVisitor extends FunBaseVisitor<Byte[]> {
    Set<String> functions = new TreeSet<>();

    @Override
    public Byte[] visitLet(FunParser.LetContext ctx) {
        LetCompiler compiler = new LetCompiler(ctx, functions);
        String functionName = compiler.getName();
        functions.add(functionName);
        byte[] classFile = compiler.compile();
        try {
            FileOutputStream out = new FileOutputStream(functionName + ".class");
            out.write(classFile);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        visit(ctx.getChild(ctx.getChildCount() - 1));

        return null;
    }


}
